package xin.clips.system.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import xin.clips.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xin.clips.common.core.text.Convert;
import xin.clips.common.utils.PinyUtils;
import xin.clips.common.utils.ProducerNumUtil;
import xin.clips.common.utils.StringUtils;
import xin.clips.system.domain.gy.GyKnowBaseMgt;
import xin.clips.system.domain.gy.GyKnowBaseType;
import xin.clips.system.mapper.gy.GyKnowBaseMgtMapper;
import xin.clips.system.service.gy.IGyKnowBaseMgtService;

/**
 * 知识管理Service业务层处理
 *
 * @author yuanzy
 * @date 2023-03-02
 */
@Service
public class GyKnowBaseMgtServiceImpl implements IGyKnowBaseMgtService {
    @Autowired
    private GyKnowBaseMgtMapper gyKnowBaseMgtMapper;

    /**
     * 查询知识管理
     *
     * @param id 知识管理主键
     * @return 知识管理
     */
    @Override
    public GyKnowBaseMgt selectGyKnowBaseMgtById(Long id) {
        return gyKnowBaseMgtMapper.selectGyKnowBaseMgtById(id);
    }

    /**
     * 查询知识管理列表
     *
     * @param gyKnowBaseMgt 知识管理
     * @return 知识管理
     */
    @Override
    public List<GyKnowBaseMgt> selectGyKnowBaseMgtList(GyKnowBaseMgt gyKnowBaseMgt) {
        return gyKnowBaseMgtMapper.selectGyKnowBaseMgtList(gyKnowBaseMgt);
    }

    /**
     * 新增知识管理
     *
     * @param gyKnowBaseMgt 知识管理
     * @return 结果
     */
    @Override
    public int insertGyKnowBaseMgt(GyKnowBaseMgt gyKnowBaseMgt) {
        Integer maxId= gyKnowBaseMgtMapper.selectMaxId() ;
        if(maxId==null){
            maxId=0;
        }
        gyKnowBaseMgt.setKnowCode(ProducerNumUtil.getId("z", (maxId+ 1)));
        gyKnowBaseMgt.setCreateTime(DateUtils.getNowDate());
        return gyKnowBaseMgtMapper.insertGyKnowBaseMgt(gyKnowBaseMgt);
    }

    /**
     * 修改知识管理
     *
     * @param gyKnowBaseMgt 知识管理
     * @return 结果
     */
    @Override
    public int updateGyKnowBaseMgt(GyKnowBaseMgt gyKnowBaseMgt) {
        gyKnowBaseMgt.setUpdateTime(DateUtils.getNowDate());
        return gyKnowBaseMgtMapper.updateGyKnowBaseMgt(gyKnowBaseMgt);
    }

    /**
     * 批量删除知识管理
     *
     * @param ids 需要删除的知识管理主键
     * @return 结果
     */
    @Override
    public int deleteGyKnowBaseMgtByIds(String ids) {
        return gyKnowBaseMgtMapper.deleteGyKnowBaseMgtByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除知识管理信息
     *
     * @param id 知识管理主键
     * @return 结果
     */
    @Override
    public int deleteGyKnowBaseMgtById(Long id) {
        return gyKnowBaseMgtMapper.deleteGyKnowBaseMgtById(id);
    }

    public boolean checkKnowNameUnique(GyKnowBaseMgt gyKnowBaseMgt) {
        Long id = StringUtils.isNull(gyKnowBaseMgt.getId()) ? -1L : gyKnowBaseMgt.getId();
        GyKnowBaseMgt info = gyKnowBaseMgtMapper.
                checkKnowNameUnique(gyKnowBaseMgt.getKnowName(), gyKnowBaseMgt.getKnowType());
        if (ObjectUtil.isNotEmpty(info) && info.getId().longValue() != id) {
            return false;
        }
        return true;
    }

    @Override
    public Integer selectMaxId() {
        return gyKnowBaseMgtMapper.selectMaxId();
    }
}
